package com.hxt.cfg.db;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.hxt.framework.mybatis.ConsoleInterceptor;
import com.hxt.framework.mybatis.IMyBatisDao;
import com.hxt.framework.mybatis.LikeInterceptor;
import com.hxt.framework.mybatis.MyBatisDaoImpl;
import com.hxt.framework.mybatis.crud.DaoImpl;
import com.hxt.framework.mybatis.crud.IDao;

@Configuration(value = Ccmfg.prefix)
@tk.mybatis.spring.annotation.MapperScan(basePackages = "com.hxt.*.dao.impl", sqlSessionFactoryRef = Ccmfg.prefix
		+ "SqlSessionFactory")
@ConfigurationProperties(prefix = Ccmfg._package + "." + Ccmfg.prefix)
public class Ccmfg {

	public static final String _package = "jiaparts.db";
	public static final String prefix = "xt";

	private String uniqueResourceName;
	private int minPoolSize;
	private int maxPoolSize;
	private int maxLifetime;
	private int borrowConnectionTimeout;
	private int maintenanceInterval;
	private String testQuery;
	private String xaDataSourceClassName;
	private XaProperties xaProperties;

	@Bean(name = Ccmfg.prefix + "DataSource", initMethod = "init", destroyMethod = "close")
	public DataSource getDataSource() {

		AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
		atomikosDataSourceBean.setUniqueResourceName(uniqueResourceName);
		atomikosDataSourceBean.setXaDataSourceClassName(xaDataSourceClassName);

		Properties properties = new Properties();
		properties.put("url", xaProperties.getUrl());
		properties.put("user", xaProperties.getUser());
		properties.put("password", xaProperties.getPassword());
		atomikosDataSourceBean.setXaProperties(properties);

		atomikosDataSourceBean.setMinPoolSize(minPoolSize);
		atomikosDataSourceBean.setMaxPoolSize(maxPoolSize);
		atomikosDataSourceBean.setBorrowConnectionTimeout(borrowConnectionTimeout);
		atomikosDataSourceBean.setTestQuery(testQuery);
		atomikosDataSourceBean.setMaintenanceInterval(maintenanceInterval);
		return atomikosDataSourceBean;
	}

	@Bean(name = Ccmfg.prefix + "SqlSessionFactory")
	public SqlSessionFactory getSqlSessionFactory(@Qualifier(Ccmfg.prefix + "DataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setTypeAliasesPackage("com.hxt.*.model");
		bean.setPlugins(new Interceptor[] { new ConsoleInterceptor(), new LikeInterceptor() });

		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		bean.setMapperLocations(resolver.getResources("classpath:mapper/**/**/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "myBatisDao_" + Ccmfg.prefix)
	public IMyBatisDao getIMyBatisDao(
			@Qualifier(Ccmfg.prefix + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
		MyBatisDaoImpl iMyBatisDao = new MyBatisDaoImpl();
		iMyBatisDao.setSqlSessionFactory(sqlSessionFactory);
		return iMyBatisDao;
	}

	@SuppressWarnings("rawtypes")
	@Bean(name = "idao_" + Ccmfg.prefix)
	public IDao getIDao(@Qualifier("myBatisDao_" + Ccmfg.prefix) IMyBatisDao iMyBatisDao) {
		DaoImpl bean = new DaoImpl();
		bean.setiMyBatisDao(iMyBatisDao);
		return bean;
	}

	public static class XaProperties {
		private String url;
		private String user;
		private String password;

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getUser() {
			return user;
		}

		public void setUser(String user) {
			this.user = user;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

	}

	public void setUniqueResourceName(String uniqueResourceName) {
		this.uniqueResourceName = uniqueResourceName;
	}

	public String getUniqueResourceName() {
		return uniqueResourceName;
	}

	public int getMinPoolSize() {
		return minPoolSize;
	}

	public void setMinPoolSize(int minPoolSize) {
		this.minPoolSize = minPoolSize;
	}

	public int getMaxPoolSize() {
		return maxPoolSize;
	}

	public void setMaxPoolSize(int maxPoolSize) {
		this.maxPoolSize = maxPoolSize;
	}

	public int getMaxLifetime() {
		return maxLifetime;
	}

	public void setMaxLifetime(int maxLifetime) {
		this.maxLifetime = maxLifetime;
	}

	public int getBorrowConnectionTimeout() {
		return borrowConnectionTimeout;
	}

	public void setBorrowConnectionTimeout(int borrowConnectionTimeout) {
		this.borrowConnectionTimeout = borrowConnectionTimeout;
	}

	public int getMaintenanceInterval() {
		return maintenanceInterval;
	}

	public void setMaintenanceInterval(int maintenanceInterval) {
		this.maintenanceInterval = maintenanceInterval;
	}

	public String getTestQuery() {
		return testQuery;
	}

	public void setTestQuery(String testQuery) {
		this.testQuery = testQuery;
	}

	public String getXaDataSourceClassName() {
		return xaDataSourceClassName;
	}

	public void setXaDataSourceClassName(String xaDataSourceClassName) {
		this.xaDataSourceClassName = xaDataSourceClassName;
	}

	public XaProperties getXaProperties() {
		return xaProperties;
	}

	public void setXaProperties(XaProperties xaProperties) {
		this.xaProperties = xaProperties;
	}
}
